package com.c611.smartclassroom.service.Impl;

import com.c611.smartclassroom.mapper.BuildMapper;
import com.c611.smartclassroom.mapper.ClassRoomMapper;
import com.c611.smartclassroom.mapper.SchoolMapper;
import com.c611.smartclassroom.mapper.WorkOrderMapper;
import com.c611.smartclassroom.model.*;
import com.c611.smartclassroom.service.WorkOrderService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class WorkOrderServiceImpl implements WorkOrderService {
    //插入方法
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    WorkOrderMapper workOrderMapper;
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    SchoolMapper schoolMapper;
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    BuildMapper buildMapper;
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    ClassRoomMapper classRoomMapper;


    @Cacheable(value = "workOrder",key = "'queryWorkOrderCount'")
    @Override
    public long queryWorkOrderCount() {
        WorkOrderExample workOrderExample = new WorkOrderExample();
        workOrderExample.createCriteria().andDelflagNotEqualTo(1);
        return workOrderMapper.countByExample(workOrderExample);
    }


    @Cacheable(value = "workOrder",key = "'queryWorkOrder::' + #page + '::' + #pageSize",unless = "#result == null ")
    @Override
    public List<WorkOrder> queryWorkOrder(Integer page, Integer pageSize) {

        //增加分页
        RowBounds rowBounds = new RowBounds((page - 1) * pageSize,pageSize);
        WorkOrderExample workOrderExample = new WorkOrderExample();

        //增加排序
        workOrderExample.setOrderByClause("repairTime desc");

        //增加置废标识不为1
        workOrderExample.createCriteria().andDelflagNotEqualTo(1);


        //根据工单中填写的学校ID获取学校名称
        SchoolExample schoolExample = new SchoolExample();


        //通过教学楼ID查找教学楼名称
        BuildExample buildExample = new BuildExample();

        //通过教室ID查找教室名称
        ClassRoomExample classRoomExample = new ClassRoomExample();

        //将查询到的数据添加至workOrderList中
        List<WorkOrder> list = workOrderMapper.selectByExampleWithRowbounds(workOrderExample,rowBounds);

        //为List集合初始化为null，并分配内存，否则抛空指针异常
        List <School> schoolList = new ArrayList<School>();
        List<Build> buildList = new ArrayList<Build>();
        List<ClassRoom> classRoomList = new ArrayList<ClassRoom>();
        for (int i = 0; i < list.size(); i++) {
            //匹配学校名称
            schoolExample.createCriteria().andSchoolidEqualTo(list.get(i).getSchoolid()).andDelflagEqualTo(0);

            schoolList.addAll(schoolMapper.selectByExample(schoolExample));
//            if (schoolList.size() <= 0) return null;
             //匹配教学楼名称
            buildExample.createCriteria().andTbuildidEqualTo(list.get(i).getTbuildid()).andDelflagEqualTo(0);
            buildList.addAll(buildMapper.selectByExample(buildExample));
            //            if (buildList.size() <= 0) return null;
            //匹配教室名称
            classRoomExample.createCriteria().andClassroomidEqualTo(list.get(i).getClassroomid()).andDelflagEqualTo(0);
            classRoomList.addAll(classRoomMapper.selectByExample(classRoomExample));
//            if (classRoomList.size() <= 0) return null;
        }
        for (int i = 0; i < list.size(); i++) {
            if(schoolList.size() > i)
            list.get(i).setSchoolid(schoolList.get(i).getSchoolname());
//            System.out.println(list.get(i).getSchoolid());
            if(buildList.size() > i)
                list.get(i).setTbuildid(buildList.get(i).getTbuildname());
//            System.out.println(list.get(i).getTbuildid());
            if(classRoomList.size() > i)
                list.get(i).setClassroomid(classRoomList.get(i).getClassroom());
//            System.out.println(list.get(i).getClassroomid());
        }

       /* for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getSchoolid());
        }*/
        return list;
//        return workOrderMapper.selectByExampleWithRowbounds(workOrderExample,rowBounds);

    }

    //根据编号查找工单
    @Cacheable(value = "workOrder",key = "'queryByWorkOrderId::' + #worderid",unless = "#result == null ")
    @Override
    public List<WorkOrder> queryByWorkOrderId(Integer worderid) {
        WorkOrderExample workOrderExample = new WorkOrderExample();
        //根据工单中填写的学校ID获取学校名称
        SchoolExample schoolExample = new SchoolExample();
        //通过教学楼ID查找教学楼名称
        BuildExample buildExample = new BuildExample();
        //通过教室ID查找教室名称
        ClassRoomExample classRoomExample = new ClassRoomExample();
        //创建构造器，匹配编号
        workOrderExample.createCriteria().andWorderidEqualTo(worderid);
        List<WorkOrder> list  = workOrderMapper.selectByExample(workOrderExample);
        //匹配学校名称
        schoolExample.createCriteria().andSchoolidEqualTo(list.get(0).getSchoolid()).andDelflagEqualTo(0);
        //匹配教学楼名称
        buildExample.createCriteria().andTbuildidEqualTo(list.get(0).getTbuildid()).andDelflagEqualTo(0);
        //匹配教室名称
        classRoomExample.createCriteria().andClassroomidEqualTo(list.get(0).getClassroomid()).andDelflagEqualTo(0);
        list.get(0).setSchoolid(schoolMapper.selectByExample(schoolExample).get(0).getSchoolname());
        list.get(0).setTbuildid(buildMapper.selectByExample(buildExample).get(0).getTbuildname());
        list.get(0).setClassroomid(classRoomMapper.selectByExample(classRoomExample).get(0).getClassroom());

        return list;
    }

    //添加工单
    @CacheEvict(value = "workOrder",allEntries = true)
    @Override
    public int addWorkOrder(WorkOrder workOrder) {
        workOrder.setRepairtime(new Date());
        return workOrderMapper.insertSelective(workOrder);
    }


    //删除工单
    @CacheEvict(value = "workOrder",allEntries = true)
    @Override
    public int delWorkOrder(Integer wOrderID) {
        WorkOrder workOrder = new WorkOrder();
        workOrder.setWorderid(wOrderID);
        workOrder.setDelflag(1);

        return workOrderMapper.updateByPrimaryKeySelective(workOrder);
    }


    //填写处理结果
    @CacheEvict(value = "workOrder",allEntries = true)
    @Override
    public int saveWorkOrderResult(Integer wOrderID ,String hResult) {

       WorkOrder workOrder = new WorkOrder();
       workOrder.setWorderid(wOrderID);
        workOrder.setHresult(hResult);

        //如果有输入字符，则代表处理完毕
        if (hResult != null){
            workOrder.setWorkstatus(2);
        }

        //当如果空字符串，则返回 处理中 ，值为 1
        if (hResult == ""){
            workOrder.setWorkstatus(1);
        }
         //添加工单的处理结果字段
        return workOrderMapper.updateByPrimaryKeySelective(workOrder);

    }


    //查询工单处理结果
    @Cacheable(value = "workOrder",key = "'queryWorkOrderResult::' + #worderid",unless = "#result == null")
    @Override
    public String queryWorkOrderResult(Integer worderid) {

//        workOrderExample.createCriteria().andDelflagNotEqualTo(1);
        return workOrderMapper.selectByPrimaryKey(worderid).getHresult();

    }

    @Cacheable(value = "workOrder",key = "'queryWorkOrderCountByName::' + #name")
    @Override
    public long queryWorkOrderCountByName(String name) {
        WorkOrderExample workOrderExample = new WorkOrderExample();
        workOrderExample.createCriteria().andApplicantLike("%" + name + "%").andDelflagNotEqualTo(1);
        return workOrderMapper.countByExample(workOrderExample);
    }

    @Cacheable(value = "workOrder",key = "'queryWorkOrderByName::' + #page + '::' + #pageSize + '::' + #name",unless = "#result == null")
    @Override
    public List<WorkOrder> queryWorkOrderByName(Integer page, Integer pageSize, String name) {
        RowBounds rowBounds = new RowBounds((page - 1) * pageSize,pageSize);
        WorkOrderExample workOrderExample = new WorkOrderExample();
        workOrderExample.setOrderByClause("repairTime desc");
        workOrderExample.createCriteria().andApplicantLike("%" + name + "%").andDelflagNotEqualTo(1);
        SchoolExample schoolExample = new SchoolExample();
        BuildExample buildExample = new BuildExample();
        ClassRoomExample classRoomExample = new ClassRoomExample();
        List<WorkOrder> list = workOrderMapper.selectByExampleWithRowbounds(workOrderExample,rowBounds);
        List <School> schoolList = new ArrayList<School>();
        List<Build> buildList = new ArrayList<Build>();
        List<ClassRoom> classRoomList = new ArrayList<ClassRoom>();
        for (int i = 0; i < list.size(); i++) {
            schoolExample.createCriteria().andSchoolidEqualTo(list.get(i).getSchoolid()).andDelflagEqualTo(0);
            schoolList.addAll(schoolMapper.selectByExample(schoolExample));
            buildExample.createCriteria().andTbuildidEqualTo(list.get(i).getTbuildid()).andDelflagEqualTo(0);
            buildList.addAll(buildMapper.selectByExample(buildExample));
            classRoomExample.createCriteria().andClassroomidEqualTo(list.get(i).getClassroomid()).andDelflagEqualTo(0);
            classRoomList.addAll(classRoomMapper.selectByExample(classRoomExample));
        }
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setSchoolid(schoolList.get(i).getSchoolname());
            list.get(i).setTbuildid(buildList.get(i).getTbuildname());
            list.get(i).setClassroomid(classRoomList.get(i).getClassroom());
        }
        return list;
    }

}
